package com.bestcem.xm.ticket.dao.converter;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.ticket.entity.mongo.Ticket;
import com.bestcem.xm.ticket.entity.mongo.TicketConfig;
import com.bestcem.xm.ticket.entity.mysql.TicketConfigDO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/18 10:10
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TicketConfigConverter {

    @Nullable
    public static TicketConfig doToDto(TicketConfigDO ticketConfigDO) {
        if (Objects.isNull(ticketConfigDO)) {
            return null;
        }

        TicketConfig ticketConfig = new TicketConfig();
        return setDto(ticketConfig, ticketConfigDO);
    }

    public static <T extends TicketConfig> T setDto(T entity, TicketConfigDO isDO) {
        if (Objects.isNull(isDO)) {
            return null;
        }

        entity.setId(isDO.getTicketConfigId());
        entity.setSysId(isDO.getSysId());
        entity.setOrgId(isDO.getOrgId());
        entity.setUserId(isDO.getUserId());
        entity.setSurveyId(isDO.getSurveyId());
        entity.setTicketFlowIds(CommonConverter.stringToList(isDO.getTicketFlowIds(), String.class));
        entity.setCreateTrigger(isDO.getCreateTrigger());
        entity.setAlertId(isDO.getAlertId());
        entity.setAlertRuleIds(CommonConverter.stringToList(isDO.getAlertRuleIds(), String.class));
        entity.setAlertRuleCount(isDO.getAlertRuleCount());
        entity.setOnlyHasContact(isDO.getOnlyHasContact());
        entity.setTicketTemplateId(isDO.getTicketTemplateId());
        entity.setDetailTemplateId(isDO.getDetailTemplateId());
        entity.setTicketNotifyConfigIds(CommonConverter
                .stringToList(isDO.getTicketNotifyConfigIds(), String.class));
        entity.setRuleList(CommonConverter.stringToList(isDO.getRuleList(), String.class));
        entity.setStatusRuleList(CommonConverter
                .stringToList(isDO.getStatusRuleList(), JSONObject.class));
        entity.setTitle(isDO.getTitle());
        entity.setState(isDO.getState());
        entity.setTicketRuleId(isDO.getTicketRuleId());
        entity.setNeedDeal(isDO.getNeedDeal());
        entity.setNeedRecord(isDO.getNeedRecord());
        entity.setRemindAdmin(isDO.getRemindAdmin());
        entity.setNotifyHandler(isDO.getNotifyHandler());
        entity.setTemplateDetailFields(CommonConverter
                .stringToList(isDO.getTemplateDetailFields(), Ticket.TemplateDetailField.class));
        entity.setSmtpId(isDO.getSmtpId());
        entity.setSample(isDO.getSample());

        CommonConverter.setEntity(entity, isDO);
        return entity;
    }

    @Nonnull
    public static List<TicketConfig> doListToDtoList(List<TicketConfigDO> ticketConfigDOList) {
        List<TicketConfig> list = new ArrayList<>();
        if (Objects.isNull(ticketConfigDOList)) {
            return list;
        }
        for (TicketConfigDO ticketConfigDO : ticketConfigDOList) {
            TicketConfig ticketConfig = doToDto(ticketConfigDO);
            list.add(ticketConfig);
        }
        return list;
    }

    @Nullable
    public static TicketConfigDO dtoToDo(TicketConfig ticketConfig) {
        if (Objects.isNull(ticketConfig)) {
            return null;
        }

        TicketConfigDO ticketConfigDO = new TicketConfigDO();
        ticketConfigDO.setTicketConfigId(ticketConfig.getId());
        ticketConfigDO.setSysId(ticketConfig.getSysId());
        ticketConfigDO.setOrgId(ticketConfig.getOrgId());
        ticketConfigDO.setUserId(ticketConfig.getUserId());
        ticketConfigDO.setSurveyId(ticketConfig.getSurveyId());
        ticketConfigDO.setTicketFlowIds(CommonConverter.listToString(ticketConfig.getTicketFlowIds()));
        ticketConfigDO.setCreateTrigger(ticketConfig.getCreateTrigger());
        ticketConfigDO.setAlertId(ticketConfig.getAlertId());
        ticketConfigDO.setAlertRuleIds(CommonConverter.listToString(ticketConfig.getAlertRuleIds()));
        ticketConfigDO.setAlertRuleCount(ticketConfig.getAlertRuleCount());
        ticketConfigDO.setOnlyHasContact(ticketConfig.getOnlyHasContact());
        ticketConfigDO.setTicketTemplateId(ticketConfig.getTicketTemplateId());
        ticketConfigDO.setDetailTemplateId(ticketConfig.getDetailTemplateId());
        ticketConfigDO.setTicketNotifyConfigIds(CommonConverter
                .listToString(ticketConfig.getTicketNotifyConfigIds()));
        ticketConfigDO.setRuleList(CommonConverter.listToString(ticketConfig.getRuleList()));
        ticketConfigDO.setStatusRuleList(CommonConverter
                .listToString(ticketConfig.getStatusRuleList()));
        ticketConfigDO.setTitle(ticketConfig.getTitle());
        ticketConfigDO.setState(ticketConfig.getState());
        ticketConfigDO.setTicketRuleId(ticketConfig.getTicketRuleId());
        ticketConfigDO.setNeedDeal(ticketConfig.getNeedDeal());
        ticketConfigDO.setRemindAdmin(ticketConfig.getRemindAdmin());
        ticketConfigDO.setNotifyHandler(ticketConfig.getNotifyHandler());
        ticketConfigDO.setTemplateDetailFields(CommonConverter.listToString(ticketConfig.getTemplateDetailFields()));
        ticketConfigDO.setSmtpId(ticketConfig.getSmtpId());
        ticketConfigDO.setSample(ticketConfig.getSample());

        ticketConfigDO.setNeedRecord(ticketConfig.getNeedRecord());

        CommonConverter.setDo(ticketConfig, ticketConfigDO);
        return ticketConfigDO;
    }
}
